﻿<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Text;
using System.Data;
using System.Data.OleDb;
public class Handler : IHttpHandler
{
    Dao.DealWith dw = new Dao.DealWith(); 
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string returnJson = string.Empty;
        string sqlexe = string.Empty;
        string action = RequestObject.RequstString("action");
           
        switch (action)
        {
            case "save":
               returnJson = Save();
                break;
            case "update":
                returnJson = Update();
                break;
            case "del":
                returnJson = Delete();
                break;
            case "list":
            case "query":
                returnJson = GetData(action);
                break;
            case "get":
                returnJson = getSingleData();
                break;
            default:
                break;
        }
        context.Response.Write(returnJson);
    }

    private string getSingleData()
    {
        string id = RequestObject.RequstString("id");
        DataTable dt = dw.SelectSingle(id);
        return Joson.onForm(dt);
    }
    private string GetData(string action)
    {
        int page = RequestObject.page;
        int rows = RequestObject.rows;
        DataTable dt;
        if (action.Equals("list"))
        {
             dt= dw.SelectALL();
        }
        else
        {
            string title = SqlLike.LikeString( RequestObject.RequstString("title"));
            dt = dw.Query(title);  
        }
        return Joson.onDataGrid(dt, page, rows);
        
    }
    private string Delete()
    {
        string returnJson = string.Empty;
        System.Data.Common.DbTransaction dbt = null;
        bool result = true;
        string idString = RequestObject.RequstString("id");
        string[] idArry = idString.Split(',');
        try
        {           
            foreach (string id in idArry)
            {
                result = result && dw.Delete(id,ref dbt);
            }

            if (result == true)
            {
                if (dbt != null) dbt.Commit();
                return returnJson = "{success:true}";
            }
            else
            {
                if (dbt != null) dbt.Rollback();
                return  returnJson = "{success:false}";
            }
        }
        catch
        {
            if (dbt != null) dbt.Rollback();
            return returnJson = "{success:false}";
        }
    }
    private string Save()
    {

        string returnJson = string.Empty;
        System.Data.Common.DbTransaction dbt = null;
        bool result = true;
        try
        {

            Bobby.Entity.Common.Detail d_Model= new Bobby.Entity.Common.Detail();
            d_Model.C_BigType = RequestObject.RequstForm("C_BigType");
            d_Model.C_SmallType = RequestObject.RequstForm("C_SmallType");
            d_Model.C_Money = float.Parse(RequestObject.RequstForm("C_Money"));
            d_Model.C_Date = DateTime.Now;   
            if (result && dw.Insert(d_Model, ref dbt))
            {
                if (dbt != null) dbt.Commit();
                return returnJson = "{success:true}";
            }
            else
            {
                if (dbt != null) dbt.Rollback();
                return returnJson = "{success:false}";
            }
            
        }
        catch
        {
            if (dbt != null) dbt.Rollback();
            return returnJson = "{success:false}";
        }
    }
    private string Update()
    {

        string returnJson = string.Empty;
        System.Data.Common.DbTransaction dbt = null;
        try
        {

            Bobby.Entity.Common.Detail d_Model = new Bobby.Entity.Common.Detail();
            d_Model.ID = long.Parse(RequestObject.RequstString("id"));
            d_Model.C_BigType = RequestObject.RequstForm("C_BigType");
            d_Model.C_SmallType = RequestObject.RequstForm("C_SmallType");
            d_Model.C_Money = float.Parse(RequestObject.RequstForm("C_Money"));
            d_Model.C_Date = DateTime.Now;         
            if (dw.Update(d_Model, ref dbt))
            {
                if (dbt != null) dbt.Commit();
                return returnJson = "{success:true}";
            }
            else
            {
                if (dbt != null) dbt.Rollback();
                return returnJson = "{success:false}";
            }
        }
        catch
        {
            if (dbt != null) dbt.Rollback();
            return returnJson = "{success:false}";
        }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}